package cn.planewar;
import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.util.Random;

public abstract class SkyObject {
    public static final int LIVE = 0;
    public static final int DEAD = 1;
    public int state = LIVE;

    private int width;
    private int height;
    private int x;
    private int y;
    private int speed;
    private int objstate;
    private int timeRunning;
    private int deadTime;

    public int getWidth() {
        return width;
    }
    public void setWidth(int width) {
        this.width = width;
    }
    public int getHeight() {
        return height;
    }
    public void setHeight(int height) {
        this.height = height;
    }
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public int getSpeed() {
        return speed;
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public int getObjstate() {
        return objstate;
    }
    public void setObjstate(int objstate) {
        this.objstate = objstate;
    }
    public int getTimeRunning() {
        return timeRunning;
    }
    public void setTimeRunning(int timeRunning) {
        this.timeRunning = timeRunning;
    }
    public int getDeadTime() {
        return deadTime;
    }
    public void setDeadTime(int deadTime) {
        this.deadTime = deadTime;
    }

    public SkyObject(int weith, int height){
        this.width = weith;
        this.height = height;
        Random rand = new Random();
        x = rand.nextInt(World.WIDTH-weith+1);
        y = -height;
        speed = rand.nextInt(2)+2;
        objstate = rand.nextInt(2);
        timeRunning = 0;
        deadTime = 0;
    }
    public SkyObject(int weith,int height,int x,int y,int speed){
        this.width = weith;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
        Random rand = new Random();
        objstate = rand.nextInt(2);
        timeRunning = 0;
        deadTime = 0;
    }

    public abstract void step();

    public abstract ImageIcon getImage();

    public boolean isLive(){
        return state == LIVE;
    }
    public boolean isDead(){
        return state == DEAD;
    }

    public Bullet shootBullet(int x,int y){
        return new Bullet(x,y);
    }
    public void goDead(){
        state = DEAD;
    }

    public void deadTimer(){
        deadTime++;
    }

    public void newObjectState(){
        if (objstate==0) {
            timeRunning = 0;
            objstate = 1;
        } else {
            timeRunning = 0;
            objstate = 0;
        }
    }
    public void stateChange(){
        timeRunning++;
        if (timeRunning>50) {
            newObjectState();
        }
    }
    public void powerChange(){
        timeRunning++;
        if (timeRunning>10) {
            newObjectState();
        }
    }
    public void paintImage(Graphics g){
            getImage().paintIcon(null,g,x,y);
    }

    public boolean isHit(SkyObject other){
        int x1 = other.x-this.width;
        int x2 = other.x+other.width;
        int y1 = other.y-this.height;
        int y2 = other.y+other.height;
        int x = this.x;
        int y = this.y;

        return x>=x1 && x<=x2 && y>=y1 && y<=y2;
    }

}
